﻿@section header
{
    <link href="~/Themes/RongJi/Style/base/css/shoppingCart.css" rel="stylesheet">
}
@using Kevin.Shop.Business.Service.Entity.Helper
@model  RiderShop.Mobile.Modle.Purchase.CheckOutModel

<div id="wizard">
    <ul class="anchor">
        <li id="step-1">
            <a rel="1" class="done" href="~/Purchase/ShoppingCart">Step 1 查看我的购物车</a>
        </li>
        <li id="step-2">
            <a rel="2" class="selected">Step 2 填写核对订单信息</a>
        </li>
        <li id="step-3">
            <a rel="3" class="disabled">Step 3 购买成功</a>
        </li>
    </ul>
    <div class="shoppingCartWrapper">
        <span class="icon ding"></span>
        <h2 class="StepTitle">填写核对订单信息</h2>
        <form id="orderInfo">
            <div class="orderInfos">
                <h4>收货信息</h4>
                <i class="zhi"></i>
                <div class="conts">
                    <ul>
                        <div id="shippingAddressList">
                            @foreach (var address in Model.UserShipingAddresses)
                            {
                                <li>
                                    <label>
                                        <input type="radio" name="ShippingId" id="ShippingId" value="@address.ShippingId" regionid="@address.RegionId" isdefault="@address.IsDefault" onclick="BalanceCompute()" @(address.IsDefault.HasValue && address.IsDefault.Value ? "checked='checked'" : "") />
                                        <span class="name">@address.ShipTo</span>
                                        <span class="add">@Html.Raw(address.RegionAddress + address.Address + "&nbsp;")</span>
                                        <span class="phone">手机：@address.CellPhone</span>
                                        <span class="phone">
                                            电话：
                                            @if (@address.TelPhone != null)
                                            {
                                                @address.TelPhone;
                                            }
                                            else
                                            {
                                                @Html.Raw("");
                                            }

                                        </span>
                                    </label>
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <span class="edit">
                                        <a href="#ShippingAddressCreator" class="addressCreate" onclick="loadAddress(@address.ShippingId)">【修改】</a>
                                    </span>
                                </li>
                            }
                        </div>
                        <li class="noadd">
                            <a id="addReceiveInfo" class="addressCreate" href="#ShippingAddressCreator" onclick="loadAddress()">【新增收货人信息】</a>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="orderInfos">
                <h4>商品清单</h4>
                <i class="wu"></i>
                <div class="cartlist">
                    <table id="tableRepList">
                        <thead>
                            <tr>
                                <th></th>
                                <th>商品名称</th>
                                <th>单价</th>
                                <th>数量</th>
                                <th>库存</th>
                                <th>小计</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.CartInfo.ProductList)
                            {
                                <tr>

                                    <td class="img">
                                        @if (item.ProductId.Trim() == "COM")
                                        {
                                            <img src="/Storage/CommResource/COM/COM_100.jpg" alt="" />
                                        }
                                        else
                                        {
                                            <img src="@item.ImageName" alt="" />
                                        }
                                    </td>
                                    <td class="pro">

                                        @item.Title

                                    </td>
                                    <td class="price">￥@item.SubTotal</td>
                                    <td class="amt">@item.Quantity</td>
                                    <td class="storage">
                                        <span class="green">@Html.Raw(item.InventoryLabel)</span>
                                    </td>
                                    <td class="summary">￥@item.SubTotal</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="orderInfos">
                <h4>配送方式</h4>
                <i class="yun"></i>
                <div class="paytype">
                    <ul>
                        @foreach (var shippingType in Model.OrderShippingType)
                        {
                            <li class="item">
                                <label>
                                    <input type="radio" id="ShippingModeId" name="ShippingModeId" value="@shippingType.ShippingModeId" onclick="BalanceCompute()" @(shippingType.ShippingModeId == 100 ? "checked" : "") />
                                    @shippingType.ShippingModeName
                                </label>
                            </li>
                        }

                    </ul>
                    <div id="selfPickAddress" class="paytype" style="display:none">
                        <h5>请选择就近提货点</h5>
                        <ul>
                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="巴布里公社-枫林绿洲店" />
                                    <span> 巴布里公社-枫林绿洲店： 科技六路东段枫林绿洲东南门内 029-65653410</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself2" name="SelfPickAddressId" value="100" shopname="巴布里公社-枫叶新都市店" />
                                    <span>巴布里公社-枫叶新都市店：沣惠南路枫叶新都市东门内 029-89529395</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself3" name="SelfPickAddressId" value="100" shopname="巴布里公社-凤城五路店" />
                                    <span>巴布里公社-凤城五路店：未央路与凤城五路十字向东500米紫薇风尚楼下 029-68977885</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself4" name="SelfPickAddressId" value="100" shopname="巴布里公社-白桦林居店" />
                                    <span>巴布里公社-白桦林居店：经开区凤城九路与文景路十字西北角白桦林居东门内步行街 029-86477395</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="巴布里公社-曲江中海观园店" />
                                    <span>巴布里公社-曲江中海观园店：曲江新区曲江大道169号中海观园A区 029-89122185</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="巴布里公社-逸翠尚府店" />
                                    <span> 巴布里公社-逸翠尚府店: 高新区锦业二路与丈八二路十字东南角 029-88810656</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="巴布里公社-永松路店" />
                                    <span>巴布里公社-永松路店: 雁塔区永松路56号3号院东门内广场 029-68992005</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="巴布里公社-电视塔店" />
                                    <span>巴布里公社-电视塔店：雁塔区长安南路与丈八东路交汇处华城万象楼下步行街12-10110 029-68977883</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="西农哥农产品超市-枫林意树店" />
                                    <span>西农哥农产品超市-枫林意树店：高新区科技六路天地源枫林意树14号楼105商铺 029-88865911</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="西农哥农产品超市-缤纷南郡店" />
                                    <span>西农哥农产品超市-缤纷南郡店：高新区丈八四路缤纷南郡11号楼105号 029-88891637</span>
                                </label>
                            </li>

                            <li class="item">
                                <label>
                                    <input type="radio" id="pickbyself1" name="SelfPickAddressId" value="100" shopname="蓉记鲜鲜鲜-凤城一路" />
                                    <span>西安市未央区凤城一路6号利君V时代B座1601 4008804365</span>
                                </label>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="orderInfos">
                <h4>支付方式</h4>
                <i class="fu"></i>
                <div class="paytype">
                    <ul>
                        <li>
                            <label>
                                <input type="radio" id="ArrivePay1" name="ArrivePay" value="1" />
                                鲜享卡货到付款
                            </label>
                            <span class="tip">先验货后使用鲜享卡付款</span>
                        </li>
                        <li>
                            <label>
                                <input type="radio" id="ArrivePay" name="ArrivePay" value="0" checked />
                                在线支付
                            </label>
                            <span class="tip">即时到账，支持绝大多数银行卡借记卡及部分银行信用卡</span>
                        </li>
                    </ul>
                </div>
            </div>

            @if (WebClientContext.Current.SiteSettings.EnableInvoice)
            {
                <div class="orderInfos">
                    <h4>发票类型</h4>
                    <i class="piao"></i>
                    <div class="conts">
                        <div trigger="click">
                            <input type="checkbox" id="IsInvoice" name="IsInvoice" value="1"><label for="IsInvoice"> 是否开发票</label>
                        </div>
                        <div id="invoiceDetail" style="display: none; margin:10px 0px 0px 0px" class="cont_sub tabc">
                            <label for="invSel">发票抬头：</label>
                            <input type="text" class="intxt" id="InvoiceTitle" name="InvoiceTitle" disabled="disabled" maxlength="20">
                            <span class="red">*</span>
                            <input type="checkbox" class="pt" id="agree" name="agree">&nbsp;<label for="agree">我已经阅读并同意<a href="http://dev.rongji365.com/Pages/Content/Affiche.aspx?type=0&amp;afid=74" target="_blank">《发票须知》</a></label>
                        </div>
                    </div>
                </div>
            }
            <div class="remark">
                <label>订单备注：</label>
                <input type="text" id="UserMemo" name="UserMemo" maxlength="40" class="intxt" placeholder="限40字以内" />
            </div>
            <div class="totals">
                @*<ul class="privilege">

                        <li class="liPointPay">
                            <a id="lkPointPay" rel="6" isdone="1" class="item done" href="javascript: void(0);">使用账户余额抵扣</a>
                            <div class="detail ml20">
                                <span class="d_title mr5">输入金额</span>
                                <input type="text" class="intxtS" id="usingPoints" name="OrderExpensePoint" maxlength="6">
                                <a class="btn_orange" id="btnUserPoints" href="javascript: void(0);"><strong>使用</strong></a>
                                <span class="mr5"> 您当前有 <strong class="red">0</strong> 元。</span>
                            </div>
                        </li>
                        <li class="liGiftCard">
                            <a id="lkGiftCard" rel="7" isdone="1" class="item done" href="javascript: void(0);">使用电子菜篮支付</a>
                            <div class="detail ml20">
                                <span class="d_title">卡号</span>
                                <input type="text" id="cardID" class="intxt" maxlength="20">
                                <span class="d_title">密码</span>
                                <input type="password" id="cardpassword" class="intxt" maxlength="15">
                                <a id="btnUseGiftCard" class="btn_orange" href="javascript: void(0);"><strong>使用</strong></a>
                                <div id="giftCardList">

                                </div>
                            </div>
                        </li>
                    </ul>*@
                <div class="totalinfo">
                    <p>
                        <span class="tit">商品小计：</span><span class="case">￥@Model.CartInfo.ProductPriceAmount.ToString("0.00")</span>
                    </p>
                    <p>
                        <span class="tit">运费金额：</span><span class="case" id="ShippingCharge">￥@Model.CartInfo.ShippingCharge.ToString("0.00")</span>
                    </p>
                    @*<p>
                            <span class="tit">礼品卡抵扣：</span><span class="case green" id="GiftCardAmount">- @Model.CartInfo.GiftCardAmount.ToString("0.00")</span>
                        </p>*@
                    <p>
                        <span class="tit">折扣小计：</span><span class="case green" id="DiscountAmount">￥- @Model.CartInfo.DiscountAmount.ToString("0.00")</span>
                    </p>
                    <p>
                        <span class="tit">余额抵扣：</span><span class="case green" id="ExpenseAmount">￥- @Model.CartInfo.ExpenseAmount.ToString("0.00")</span>
                    </p>
                    <p class="all">
                        <span class="tit">应付金额：</span><span class="case red" id="PayAmount">￥@Model.CartInfo.PayAmount</span>
                    </p>

                </div>
            </div>
        </form>
        <div class="actionBar">
            <button type="button" class="checkoutBtn" onclick="submitOrder()">提交订单</button>
            <a href="~/Purchase/ShoppingCart" class="goOnBtn"><i class="fa fa-caret-left"></i> 返回购物车</a>
            <span class="allPayM">应付金额：<span id="PayAmountS" class="red">￥@Model.CartInfo.PayAmount</span></span>
            <span id="lbSubmitting"><i class="fa fa-refresh fa-spin"></i> 正在提交数据,请稍候...</span>
        </div>
    </div>
</div>

<div id="ShippingAddressCreator" style="display:block">
</div>
@section footer
{
    <script language="javascript" type="text/javascript">
        var validPoint = 0;
        var order;
        var userPoint = @WebClientContext.Current.CurrentUser.Points;
        var isInvioce=false;
        var invioceAgree = false;
        $(document).ready(function () {
            $('#lbSubmitting').hide();
            order = { "ArrivePay": 0, "DiscountAmount": 0, "GiftCardList": [], "OrderExpensePoint": 0, "OrderGiftCardAmount": 0, "OrderPayAmount": 0, "OrderStatus": 0, "OrderTotalAmount": 0, "PaymentType": 1, "ProductList": [], "ReceiveAddress": "", "ReceiveCellPhone": "", "ReceiveDate": null, "ReceivePhone": "", "ShippingCharge": 0, "ShippingId": 0, "UserMemo": "" };
            $('#cardID').val('');
            $('#cardpassword').val('');
            BalanceCompute();
            $('#IsInvoice').change(function () {
                isInvioce = !isInvioce;
                if (isInvioce) {
                    $('#invoiceDetail').show();
                }
                else {
                    $('#invoiceDetail').hide();
                }
            });
            $('input#agree').change(function () {
                invioceAgree = !invioceAgree;
                if (invioceAgree) {
                    $('#InvoiceTitle').removeAttr("disabled");
                }
                else {
                    $('#InvoiceTitle').attr("disabled", "disabled");
                    $('#InvoiceTitle').val("");
                }
            });

            $('#lkPointPay').click(function () {
                var div = $('.liPointPay div.detail');
                if ($(div).css('display') == 'none')
                    $(div).show();
                else {
                    $(div).hide();
                }
            });
            $('#lkGiftCard').click(function () {
                var div = $('.liGiftCard div.detail');
                if ($(div).css('display') == 'none')
                    $(div).show();
                else {
                    $(div).hide();
                }
            });

            $('#btnUserPoints').click(function () {
                UsePoints();
            });
            $('#btnUseGiftCard').click(function () {
                UseGiftCard();
            });

            var fancyOption = {
                maxWidth: 1000,
                maxHeight: 600,
                fitToView: true,
                width: 700,
                height: 450,
                wrapCSS: 'custom-fancy-wrapper',
                helpers: {
                    overlay: {
                        css: {
                            'background': 'rgba(255,255,255,.6)'
                        },
                        closeClick: false
                    }
                },
                tpl: {
                    closeBtn: '<i class="fa fa-times"></i>'
                },
                autoSize: false,

            };
            $('.addressCreate').fancybox(fancyOption);
        });



        var UsePoints = function () {

            var points = $('#usingPoints').val();
            if (points == '') {
                KevinShop.MessageBox.Alert("请输入余额");
                return;
            }
            if ($("input[name='ShippingId']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择收货人");
                return;
            }
            if ($("input[name='ShippingModeId']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择配送方式");
                return;
            }
            if (!checkInteger(points)) {
                KevinShop.MessageBox.Alert("请输入正确的余额,使用余额只能以元为单位");
                $('#usingPoints').val(validPoint);
                return;
            }
            if (points > userPoint) {
                KevinShop.MessageBox.Alert("您的余额不足，请输入有效余额");
                $('#usingPoints').val(validPoint);
                return;
            }
            if (points > 0 && !openSMSWindow()) {
                $('body,html').animate({ scrollTop: 200 }, 1000);
                return;
            }
            validPoint = points;
            Kevin.AjaxMethod.BalanceComputeWithPoint(points, function (data) {
                RefreshOrderAmount(data);
            });
        };

        var DeleteGiftCard = function (gcNumber) {
            Kevin.AjaxMethod.DeleteGiftCard(gcNumber, function (data) {
                if (data.error) {
                    KevinShop.MessageBox.Alert(data.error);
                    return;
                }
                RefreshGiftCardList(data.value);
            });
        };

        var UseGiftCard = function () {

            if ($("input[name='ShippingId']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择收货人");
                return;
            }
            if ($("input[name='ShippingModeId']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择配送方式");
                return;
            }
            var gcNumber = $('#cardID').val();
            var gcPwd = $('#cardpassword').val();
            if (gcNumber.length == 0) {
                KevinShop.MessageBox.Alert("劵号不能为空");
                return;
            }
            if (gcPwd.length == 0) {
                KevinShop.MessageBox.Alert("密码不能为空");
                return;
            }
            if (isNaN(gcNumber) || isNaN(gcPwd)) {
                KevinShop.MessageBox.Alert("劵号或密码格式错误，请重新输入");
                return;
            }
            Kevin.AjaxMethod.UseGiftCard(gcNumber, gcPwd, function (data) {
                if (data.error) {
                    KevinShop.MessageBox.Alert(data.error);
                    return;
                }
                var result = KevinShop.Common.EvalJSON(data.value);
                if (result['Info'] != null && result['Info'] != "") {
                    KevinShop.MessageBox.Alert(result['Info']);
                    return;
                }
                //刷新礼品劵列表
                RefreshGiftCardList(result['Data']);
            });
        };

        var RefreshGiftCardList = function (html) {
            var div = $('#giftCardList');
            $(div).empty();
            $(div).html(html);
            //刷新订单金额
            Kevin.AjaxMethod.RefreshAmount(function (data) {
                RefreshOrderAmount(data);
            });
        };

        var BalanceCompute = function () {
            if ($("input[name='ShippingModeId']:checked").length != 0){
                var shiptype = $("input[name='ShippingModeId']:checked").val();

                //显示自提点
                if(shiptype=="101"){
                    $("#selfPickAddress").show();
                }
                else{
                    $("#selfPickAddress").hide();
                }

                if( $("input[name='ShippingId']:checked").length != 0) {
                    //var regionId = $("input[name='ShippingId']:checked").attr('regionId');
                    var shippingId = $("input[name='ShippingId']:checked").attr('value');

                    var url = "OrderBalanceCompute?shiptype=" + shiptype + "&shippingId=" + shippingId;
                    KevinShop.Common.Ajax.Get(url, function (data) {
                        RefreshOrderAmount(data);
                    });
                }
            }
        };

        var RefreshOrderAmount = function (currentData) {

            $('#ShippingCharge').text('￥' + (currentData['ShippingCharge']).toFixed(2));
            $('#DiscountAmount').text('￥' + (currentData['DiscountAmount']).toFixed(2));
            $('#PayAmount').text('￥' + (currentData['PayAmount']).toFixed(2));
            $('#PayAmountS').text('￥' + (currentData['PayAmount']).toFixed(2));
            $('#GiftCardAmount').text('￥' + (currentData['GiftCardAmount']).toFixed(2));
            $('#ExpenseAmount').text('￥' + (currentData['ExpenseAmount']).toFixed(2));
        };

        var multiSubmit = 0;
        submitOrder = function () {
            if (multiSubmit == 1) {
                return;
            }
            var points = 0;
            //检查
            KevinShop.Common.ShoppingCart.RefreshTotalQty();
            if (KevinShop.Common.ShoppingCart.TotalQty == 0) {
                KevinShop.MessageBox.Alert("购物车至少得有一个商品");
                return;
            }
            if($('#usingPoints')&&$('#usingPoints').val()){
                points = $('#usingPoints').val();
                if (validPoint != points) {
                    KevinShop.MessageBox.Alert("当您想使用账户余额抵扣订单金额，别忘了点击旁边的“使用”按钮，方能生效，谢谢");
                    return;
                }
            }
            if ($("input[name='ShippingId']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择收货人");
                return;
            }
            if ($("input[name='ShippingModeId']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择配送方式");
                return;
            }

            var shiptype = $("input[name='ShippingModeId']:checked").val();
            if(shiptype=="101"){
                if ($("input[name='SelfPickAddressId']:checked").length == 0) {
                    KevinShop.MessageBox.Alert("请选择自提地点");
                    return;
                }
            }

            if ($("input[name='ArrivePay']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择支付方式");
                return;
            }
            if (isInvioce) {
                if (!$('#InvoiceTitle').val()) {
                    KevinShop.MessageBox.Alert("请填写发票抬头");
                    return;
                }
            }
            if (points > 0 && !openSMSWindow()) {
                $('body,html').animate({ scrollTop: 200 }, 1000);
                return;
            }
            //获取form表单
            var orderdata = KevinShop.Common.FormToObj($('#orderInfo'), order);

            //自提-
            if (shiptype === "101") {
                var newdata = KevinShop.Common.EvalJSON(orderdata);
                var regionText = $("input[name='SelfPickAddressId']:checked").parent().children('span').html();
                newdata.UserMemo = (newdata.UserMemo ? newdata.UserMemo : "") + ' 自提地点：'+regionText;
                orderdata = KevinShop.Common.ToJSON(newdata);
            }

            //由于FormToObj无法解析
            if ($('#IsInvoice')&&isInvioce) {
                var jsonObj = KevinShop.Common.EvalJSON(orderdata);
                jsonObj["IsInvoice"] = "1";
                orderdata = KevinShop.Common.ToJSON(jsonObj);
            }
            $('#lbSubmitting').show();
            multiSubmit = 1;
            KevinShop.Common.Ajax.Post("SubmitOrder", escape(orderdata), function (result) {

                if (!result['Status'] || result['ErrorMessage']) {
                    KevinShop.MessageBox.Alert(result['ErrorMessage']);
                    $('#lbSubmitting').hide();
                    multiSubmit = 0;
                    return;
                }
                else {
                    window.location.href = "/Purchase/OrderCompleted?par=" + result.Tag;
                }
            });
        };

        loadAddress = function(addressId){
            addressId = addressId||0;
            KevinShop.Common.Ajax.Get("/MyAccount/ShippingAddressPartial?shippingId="+addressId+"&time="+new Date(), function (html) {
                $("#ShippingAddressCreator").html(html);
                $("#ShippingAddressCreator").show();
            });
        }
    </script>
}
